using Eno.Application.Dto;
using Eno.Domain.Entity.App;
using Eno.Domain.Repositories;
using Eno.Infrastructure.Tools;
using Microsoft.VisualBasic;

namespace Eno.Application.Services;

public class DepartmentService
{
    private readonly IDepartmentRepository<AppDepartment> _rep;

    public DepartmentService(IDepartmentRepository<AppDepartment> departmentRepository)
    {
        _rep = departmentRepository;
    }

    public async Task<dynamic> GetDepartmentAsync()
    {
        var list = await _rep.GetAppDepartmentsAsync();
        if (list == null)
        {
            return Tips<string>.Fail("");
        }
        return Tips<IEnumerable<AppDepartment>>.Success(list, "查询部门操作成功");
    }

    public async Task<dynamic> GetAppDepartmentByIdAsync(Guid DepartmentId)
    {
        var department = await _rep.GetAppDepartmentByIdAsync(DepartmentId);
        if (department == null)
        {
            return Tips<string>.Fail("");
        }
        return Tips<AppDepartment>.Success(department);
    }

    public async Task<dynamic> DeleteDepartmentAsync(Guid DepartmentId)
    {
        var number = await _rep.DeleteDepartmentAsync(DepartmentId);
        if (number == 0) return Tips<string>.Fail("");
        return Tips<int>.Success(number, "删除部门操作成功");
    }

    public async Task<dynamic> UpdateDepartmentAsync(Guid departmentId, DepartmentDto departmentDto)
    {
        var obj = _rep.GetAppDepartmentByIdAsync(departmentId);
        if (obj == null) return Tips<string>.Fail("");
        var newDepartment = new AppDepartment
        {
            ID = departmentId,
            DepartName = departmentDto.DepartmentName,
            ParentID = departmentDto.ParentId,
            IsActived = departmentDto.IsActived,
            UpdatedAt = DateTime.UtcNow
        };
        var number = await _rep.UpdateDepartmentAsync(newDepartment);
        return Tips<int>.Success(number, "修改部门成功");
    }

    public async Task<dynamic> AdditionDepartmentAsync(DepartmentDto departmentDto)
    {
        var department = await _rep.GetAppDepartmentsAsync();
        var obj = department.Any(d => d.DepartName == departmentDto.DepartmentName);
        if (obj) return Tips<string>.Fail("");
        var newDepartment = new AppDepartment
        {
            ID = Guid.NewGuid(),
            DepartName = departmentDto.DepartmentName,
            ParentID = departmentDto.ParentId,
            IsActived = departmentDto.IsActived,
            IsDeleted = false,
            CreatedAt = DateTime.UtcNow,
            UpdatedAt = DateTime.UtcNow
        };
        var number = await _rep.AdditionDepartmentAsync(newDepartment);
        return Tips<int>.Success(number, "新增部门成功");
    }

    public async Task<dynamic> ToggleDepartmentStatusAsync(Guid departmentId)
    {
        var department = await _rep.GetAppDepartmentByIdAsync(departmentId);
        if (department == null)
        {
            return Tips<string>.Fail("", 3474, "部门不存在");
        }

        // 切换状态
        department.IsActived = !department.IsActived;
        department.UpdatedAt = DateTime.UtcNow;

        var number = await _rep.UpdateDepartmentAsync(department);
        if (number == 0)
        {
            return Tips<string>.Fail("", 3474, "状态更新失败");
        }

        var statusText = department.IsActived ? "启用" : "禁用";
        return Tips<int>.Success(number, $"部门{statusText}成功");
    }
}